package com.szly.phm.admin.utils;

import com.szly.phm.admin.security.LoginUser;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * Security工具类
 * @author chengjiang
 * @date
 */
public class SecurityUtils {

    /**
     * 密码加密
     * @author chengjiang
     */
    public static String encryptPassword(String password) {
        var passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.encode(password);
    }

    /**
     * 判断密码是否相同
     * @param rawPassword 真实密码
     * @param encodedPassword 加密后字符
     */
    public static boolean matchesPassword(String rawPassword, String encodedPassword) {
        var passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.matches(rawPassword, encodedPassword);
    }

    /**
     * 获取登录用户信息
     * @author chengjiang
     * @date 2021/3/30
     */
    public static LoginUser getLoginUser() {
        System.out.println(SecurityContextHolder.getContext().getAuthentication().getPrincipal());
//        return (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return new LoginUser();
    }
}
